<template>
	<view class="page">
        <!-- #ifdef MP-WEIXIN --> 
		<top-nav :inner-title="title" is-show-back="false" is-fixed="false" :navH="navH" :n="n" is-white="bg-white" opacity="1"></top-nav>
         <!-- #endif -->
         <!-- #ifdef MP-WEIXIN -->
         <button class='head' open-type="getUserInfo" @getuserinfo="getUserInfo">
         	<view class="head_content">
         		<view class='face'>
         		  <image :src='face?face:"../../static/face13x.png"'></image>
         		</view>
         		<view class='nickname'>
         			<view>{{nickName?nickName:"同学快跑"}}</view>
         			<view class='slogan'>学习再累也要记得吃饭哟～</view>
         		</view>
         		<icon class="arr_right"></icon>
         	</view>
         </button>
         <!-- #endif -->
         <!-- #ifdef MP-ALIPAY -->
         <button class='head' open-type="getAuthorize" @getAuthorize="onGetAuthorizeInfo" @error="onAuthErrorInfo" scope='userInfo'>
         	<view class="head_content">
         		<view class='face'>
         		  <image :src='face?face:"../../static/face13x.png"'></image>
         		</view>
         		<view class='nickname'>
         			<view>{{nickName?nickName:"同学快跑"}}</view>
         			<view class='slogan'>学习再累也要记得吃饭哟～</view>
         		</view>
         		<icon class="arr_right"></icon>
         	</view>
         </button>
         <!-- #endif -->
		
		<view class='list'>
            <block v-if="isLogin">
                
                <block v-if="!isRegisted">
                    <!-- #ifdef MP-WEIXIN -->
                        <button class="item gray_text red_text" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
                            <view class='icon'>
                            	<icon class='packet'></icon>
                            </view>
                            <view class='text'>
                            	<text>我的红包</text>
                                <view class="red_bg_text flex"><view class="white_packet"></view>绑定手机号查看红包</view>
                            </view>
                        </button>  
                    <!-- #endif -->
                    <!-- #ifdef MP-ALIPAY -->
                        <button class="item gray_text red_text" open-type="getAuthorize" @getAuthorize="onGetAuthorize" @error="onAuthError" scope='phoneNumber'>
                            <view class='icon'>
                            	<icon class='packet'></icon>
                            </view>
                            <view class='text'>
                            	<text>我的红包</text>
                                 <view class="red_bg_text flex"><view class="white_packet"></view>绑定手机号查看红包</view>
                                 
                            </view>
                        </button>  
                    <!-- #endif --> 
                </block>
                <block v-else>
                    <view class="item"  @click='goPacket()'>
                        <view class='icon'>
                        	<icon class='packet'></icon>
                        </view>
                        <view class='text'>
                        	<text>我的红包</text>
                             <view class="gray_text flex"> 
                                    <block v-if="redpacket_count>0">
                                        <text>{{redpacket_count }}</text> 个未使用
                                    </block>
                                    <block v-else>
                                        无可用红包
                                    </block>
                                    <icon class="arr_right"></icon>
                             </view> 
                        </view>
                    </view>
                </block>
            </block>
          
 
			<view class='item' v-if="isLogin">
				<view class='icon'>
					<icon class='poi'></icon>
				</view>
				<view class='text' @click='goAddress()'>
					<text>我的收货地址</text>
                   <icon class="arr_right"></icon>
					
				</view>
			</view>
			<view class='item'>
				<view class='icon'>
					<icon class='contact'></icon>
				</view>
				<view class='text' @click='goContact()'>
					<text>我的客服</text>
					<icon class="arr_right"></icon>
				</view>
			</view>

			<view class='item last'>
				<view class='icon'>
					<icon class='feedback'></icon>
				</view>
				<view class='text' @click='goFeedback()'>
					 <text>意见反馈</text>
					 <icon class="arr_right"></icon>
				</view>
			</view>
		</view>
        <view class="shadow_out" :style="{height:showContact}">
                <view class="shadow" @click='closeShadow()'></view>
                <view class="content bottom_fixed">
                    <!-- #ifdef MP-WEIXIN -->
                    <button class="item" open-type="contact">
                        <view class='icon'>
                            <icon class='feedback-innder'></icon>
                        </view>
                        <view class='text'>
                            <view class="badge_out">
                                <text>在线客服</text>
                                <view class="badge">推荐</view>
                            </view>
                            <icon class="arr_right"></icon>
                        </view>
                    </button>
                    <!-- #endif -->
                    <view class='item'>
                        <view class='icon'>
                            <icon class='phone'></icon>
                        </view>
                        <view class='text' @click='goMobile()'>
                            <text>客服电话   17621301616</text>
                            <icon class="arr_right"></icon>
                        </view>
                    </view>
                </view>
        </view>
	</view>
</template>

<script>
    import uniFly    from 'unifly';
	import request   from '../../common/request.js';
	import $api_base from '../../api/api_base.js';
	import topNav    from '@/template/topnav/index.vue';
	import event     from '../../common/event.js';
	export default {
		components: { topNav },
		data() {
			return {
				userInfo: {},
				navH: 0,
				n: 0,
				nickName:'',
				face:'',
				title:'我的',
				isLogin: false,
                showContact: 0,
                redpacket_count: 0,
                isRegisted: false,
                bind_phone: 0
			}
		},
		onLoad: function (options) {
			this.navH	    = getApp().globalData.navHeight;
			this.n		    = getApp().globalData.n;
			this.userInfo   = getApp().globalData.userInfo;
			this.isLogin    = getApp().isLogin;
			this.kfPhone    = getApp().kfPhone;
			this.nickName   = getApp().globalData.userInfo.nickName;
			this.face	    = getApp().globalData.userInfo.face;
            this.isRegisted = getApp().globalData.isRegisted;
            this.bind_phone = getApp().globalData.userInfo.bind_phone || 0;  
		},
        onShow: function(){
            this.loadPacket();
        },
		methods:{
            loadPacket: function(){
                var self = this;
                var param = {
                    u_id     : uni.getStorageSync('openid'),
                    p_type   : 2,
                    p_status : 1,
                    page     : 0,
                    limit    : 30
                };
                $api_base.post($api_base.getRedpacketList,param).then(function(res) {
                    let data = res.data;
                    if (data.ok == 1) {
                        console.log(data);
                        self.redpacket_count = Number(data.data.redpacket_count) + Number(data.data.store_redpacket_count);
                    }else{
                        self.redpacket_count = 0;
                    }
                })
            },
            goPacket: function () {
				uni.navigateTo({
					url: '/pages/red-packet/index'
				});
			},
			goAddress: function () {
				uni.navigateTo({
					url: '/pages/address/index'
				});
			},
			goFeedback:function () {
				uni.navigateTo({
					url: '/pagesOther/feedback/index'
				});
			},
			goContact: function () {
				this.showContact = '100%';
			},
            closeShadow: function(){
                this.showContact = 0;
            },
			openLogin: function () {

			},
            goMobile: function () {
                uni.makePhoneCall({
                    phoneNumber: getApp().kfPhone
                });
            },
            
			getUserInfo() {//同意授权，获取用户信息，encryptedData是加密字符串，里面包含unionid和openid信息
                var self = this;
				var openid = uni.getStorageSync('openid');  //获取本地缓存
                uni.getProvider({
                    service: 'oauth',
                    success: function (red) {
                        let provider = red.provider;
                        uni.getUserInfo({
                            // #ifdef MP-WEIXIN
                            withCredentials: true,//此处设为true，才会返回encryptedData等敏感信息
                            // #endif
                            provider:provider,
                        	success: res => {
                                console.log(res);
                        		self.nickName = res.userInfo.nickName;
                        		self.face	  = res.userInfo.avatarUrl;
                        		self.gender   = res.userInfo.gender;
                        		$api_base.post($api_base.getUser,{ nickname: res.userInfo.nickName, face: res.userInfo.avatarUrl }).then(function(res){
                        			let data = res.data;
                        			if (data.ok == 1) {
                        
                        			}
                        		});  
                        	}
                        });
                    },
                })	
			},
            getPhoneNumber: function(e){
                var self = this;
                if(getApp().globalData.userInfo.bind_phone == 1){
                    self.loadNewPacket(2);
                }else{
                    var schoolId = getApp().globalData.schoolId;
                    if (e.detail.errMsg == 'getPhoneNumber:ok') {
                        $api_base.post($api_base.getDecryptData,{encryptedData: e.detail.encryptedData, iv: e.detail.iv,school_id: schoolId}).then(function (e) {
                            let data = e.data;
                            if(data.ok == 0){
                                self.isRegisted = false;
                                getApp().globalData.isRegisted = self.isRegisted;
                                uni.showModal({
                                 	title: '',
                                 	content: '注册失败.',
                                 	showCancel: false,
                                 	confirmText: '知道了',
                                 	confirmColor: '#00B89D',
                                 	success(res) {
                                 		if (res.confirm) {
                                 			console.log('用户点击确定')
                                 		} else if (res.cancel) {
                                 			console.log('用户点击取消')
                                 		}
                                 	}
                                })
                            }else{ 
                                self.isRegisted  = true;
                                self.bind_phone  = 1;
                                getApp().globalData.userInfo.bind_phone = 1;
                                getApp().globalData.isRegisted = self.isRegisted;
                                uni.showToast({
                                    title:'注册成功'
                                })
                                self.loadNewPacket(2);
                            }
                        })
                    }else{
                        uni.navigateTo({
                            url:'/pages/index/bind-phone/index?from=mine'
                        })
                        //self.showLoginoutTip = true;
                        getApp().globalData.isRegisted = self.isRegisted;
                    }
                } 
            },
 
            loadNewPacket: function(n){
                var self = this;
                var u_id = uni.getStorageSync('openid');
                var r_type = n;
                var school_id = getApp().globalData.schoolId;
                $api_base.post($api_base.getRedpackeNew,{u_id:u_id,r_type:r_type,school_id:school_id},1).then(function(res) {
                	var data = res.data;
                    if(data.ok==1){
                        self.loadPacket();
                    }
                })
            },
            // #ifdef MP-ALIPAY
            onGetAuthorizeInfo(){
                var self = this;
                my.getAuthCode({
                  scopes: 'auth_user',
                  success: (res) => {
                    my.getAuthUserInfo({
                      success: (userInfo) => {
                          console.log(userInfo);
                        self.nickName = userInfo.nickName;
                        self.face	  = userInfo.avatar;
                        $api_base.post($api_base.getUser,{ nickname: self.nickName, face: self.face }).then(function(red){
                        	let data = red.data;
                        	if (data.ok == 1) {
                                                
                        	}
                        });  
                        
                      }
                    });
                  },
                });
            },
            onGetAuthorize: function(res){
                var self = this;
                
                if(getApp().globalData.userInfo.bind_phone == 1){
                    self.loadNewPacket(2);
                }else{
                    my.getPhoneNumber({
                        success: (res) => {
                            let encryptedData = res.response;
                            console.log(res);
                            $api_base.post($api_base.getAliDecryptData,res).then(function (e) {
                                let data = e.data;
                                if(data.ok == 0){    
                                    self.isRegisted = false;
                                    getApp().globalData.isRegisted = self.isRegisted;
                                    uni.showModal({
                                     	title: '',
                                     	content: '注册失败.',
                                     	showCancel: false,
                                     	confirmText: '知道了',
                                     	confirmColor: '#00B89D',
                                     	success(res) {
                                     		if (res.confirm) {
                                                
                                     			console.log('用户点击确定')
                                     		} else if (res.cancel) {
                                     			console.log('用户点击取消')
                                     		}
                                     	}
                                    })
                                }else{ 
                                    self.loadNewPacket(2);
                                    self.isRegisted  = true;
                                    self.bind_phone  = 1;
                                    getApp().globalData.userInfo.bind_phone = 1;
                                    getApp().globalData.isRegisted = self.isRegisted;
                                    uni.showToast({
                                        title:'注册成功'
                                    })
                                }
                            })
                        },
                        fail: (res) => {
                            console.log(res);
                            console.log('getPhoneNumber_fail');
                            getApp().globalData.isRegisted = self.isRegisted;
                        },
                    });
                }
               
            },
            onAuthError(e) {  
                console.error(e);  
                uni.navigateTo({
                    url:'/pages/index/bind-phone/index?from=mine'
                }) 
                getApp().globalData.isRegisted = self.isRegisted;
            },
            // #endif
            
		}
	}
</script>
<style>
  page {
  background-color: #f9f9f9;
  }
</style>
<style lang="scss" scoped>
	@import 'index.scss';
</style>
